{% extends 'base.html' %}

{% block title %}编辑日志{% endblock %}

{% block before_head %}

<script>

var
    action = '{{ action }}',
    redirect = '{{ redirect }}';

function initVue(blog) {
    var vm = new Vue({
        el: '#form_blog',
        data: blog,
        methods: {
            submit: function (event) {
                event.preventDefault();
                startLoading();
                postApi(action, this.$data, function (err, r) {
                    if (err) {
                        showError(err);
                    }
                    else {
                        return location.assign(redirect);
                    }
                    stopLoading();
                });
            },
            cancel: function() {
                location.assign(redirect);
            }
        }
    });
}

 {% if id %}

$(function () {
    startLoading();
    getApi('/api/blogs/{{ id }}', function (err, blog) {
        if (err) {
            showError(err);
        }
        else {
            stopLoading();
            initVue(blog);
        }
    });
});

 {% else %}

$(function () {
    initVue({
        title: '',
        summary: '',
        content: ''
    });
});

 {% endif %}

</script>

{% endblock %}

{% block content %}

    <div class="uk-width-2-3 uk-margin-bottom">
        <div class="uk-panel uk-panel-box">
            <h3>编辑日志</h3>
        </div>
    </div>

    <div class="uk-width-2-3">
        <form id="form_blog" v-on="submit: submit" class="uk-form uk-form-stacked">
            <div class="uk-form-row">
                <label class="uk-form-label">标题:</label>
                <div class="uk-form-controls">
                    <input v-model="title" name="title" type="text" placeholder="标题" class="uk-width-1-1">
                </div>
            </div>
            <div class="uk-form-row">
                <label class="uk-form-label">摘要:</label>
                <div class="uk-form-controls">
                    <textarea v-model="summary" rows="4" name="summary" placeholder="摘要" class="uk-width-1-1" style="resize:none;"></textarea>
                </div>
            </div>
            <div class="uk-form-row">
                <label class="uk-form-label">内容:(<a href="https://www.zybuluo.com/mdeditor">markdown</a>格式)</label>
                <div class="uk-form-controls">
                    <textarea v-model="content" rows="16" name="content" placeholder="内容" class="uk-width-1-1" style="resize:none;"></textarea>
                </div>
            </div>
            <div class="uk-form-row">
                <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-save"></i> 保存</button>
                <button v-on="click: cancel()" type="button" class="uk-button"><i class="uk-icon-times"></i> 取消</button>
            </div>
            <div class="uk-alert uk-alert-danger uk-hidden"></div>
        </form>
    </div>

{% endblock %}